package de.tum.team26.eistpoll.client.UI;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.team26.eistpoll.client.EIST_POLL;

/** 
 * Class to Show the Results
 */
public class Result extends UIElement{
	
	VerticalPanel resultsPanel = new VerticalPanel(); // Poll Container
	// Result UI
	HTML quotations;
	HTMLPanel quotation = new HTMLPanel(" ");
	FlexTable flexTableLike = new FlexTable();
	FlexTable flexTableDisLike = new FlexTable();
	Label lblheadding;
	Label lblOption = new Label("How statisfied are you with today's lecture?");
	Label lblLike = new Label("What did you like most?");
	Label lblDislike = new Label("What did you dislike or did not understand at all?");
	Label lblErrMsg;
	Label btnReset = new Label("Reset");
	Label lblLine = new Label(" | ");
	Label lblLine2 = new Label(" | ");
	Label btnDelete = new Label("Delete");
	Label btnSetActive;
	
	HorizontalPanel headder = new HorizontalPanel();
	Image tum = new Image();
	Image info = new Image();
	
	private int id;
	private boolean isLecturer;
	EIST_POLL controller;
	
	public Result(final EIST_POLL controller, int id, String title, boolean lecturer, boolean isActive){
		this.id=id;
		this.isLecturer = lecturer;
		this.controller = controller;
		headder.setStyleName("headder");
		tum.setStyleName("tumlogo");
		info.setStyleName("infologo");
		tum.setUrl("tum.gif");
		info.setUrl("info.gif");
		resultsPanel.addStyleName("containeros");
		// add UI Elements
		if (lecturer) {
			if (isActive) btnSetActive = new Label("Deactivate");
			else  btnSetActive = new Label("Activate");
			btnReset.addStyleName("btlogin");
			btnSetActive.addStyleName("btlogin");
			btnDelete.addStyleName("btlogin");
			headder.add(btnReset);
			headder.add(lblLine);
			headder.add(btnSetActive);
			headder.add(lblLine2);
			headder.add(btnDelete);
			btnReset.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					reset();
				}
			});
			btnSetActive.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					setActive();
				}
			});
			btnDelete.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					delete();
				}
			});
		}
		headder.add(tum);
		headder.add(info);
		resultsPanel.add(headder);
		
		lblheadding = new Label(title);
		lblheadding.addStyleName("h1");
		resultsPanel.add(lblheadding);
		lblOption.addStyleName("h2");
		lblLike.addStyleName("h2");
		lblDislike.addStyleName("h2");
		
		if (!lecturer){
			resultsPanel.add(lblOption);
			resultsPanel.add(quotation);
		}
		else{
			resultsPanel.add(lblOption);
			resultsPanel.add(quotation);
			resultsPanel.add(lblLike);
			resultsPanel.add(flexTableLike);
			resultsPanel.add(lblDislike);
			resultsPanel.add(flexTableDisLike);
		}
		
		
	}
	
	// Getter and Setter as always:
	
	private void reset(){
		controller.resetStatistic(this.id);
	}
	
	private void setActive(){
		controller.switchPollActive(this.id);
	}
	
	private void delete(){
		controller.deletePoll(this.id);
	}
	
	@Override
	public VerticalPanel getContainer() {
		return resultsPanel;
	}
	@Override
	public void displayErrorMessage(String message) {
		if (lblErrMsg == null){
			lblErrMsg = new Label(message);
			lblErrMsg.setStyleName("error");
			resultsPanel.add(lblErrMsg);
		}
		else{
			lblErrMsg.setText(message);
		}
	}
	
	public void updateResults (int[] options){
		
		int counter = 1;
		int width=0;
		counter = options[0] + options[1] + options[2] + options[3] + options[4]; 
		String [] temp = new String [5];
		temp[0] = "Very Satisfied";
		temp[1] = "Satisfied";
		temp[2] = "Neutral";
		temp[3] = "Unsatisfied";
		temp[4] = "Very Unsatisfied";
		String bar = "<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\" align=\"left\" class=\"tblresults\">"+"<tbody>";
			for (int i=0;i<5;i++){
				width = (int) (100.0 * ((float) options[i]) / ((float) counter));
				bar = bar + "<tr>";
				bar = bar + "<td align=\"left\" class=\"tblresultscell\" \">" + temp[i] + "</td>";
				bar = bar + "<td class=\"tblresultscell\" title=\"\"><div class=\"bar\"><div class=\"inner\" style=\"width:" + width +"%;\"></div></div></td>";
				bar = bar + "<td align=\"right\" class=\"tblresultscell\">" + options[i] + "</td>";
				bar = bar + "</tr>";
			}
			bar = bar + "</tbody></table>";
		quotations = new HTML(bar, true);
		quotation.clear();
		quotation.add(quotations);	
	}

	public void updateResults (int[] options, String[] like, String[] dislike){
		updateResults(options);
		flexTableLike.removeAllRows();
		if (isLecturer){
			for (int i=0; i<like.length;i++){
				flexTableLike.setText(i, 0, like[i]);
			}
		}
		flexTableDisLike.removeAllRows();
		if (isLecturer){
			for (int i=0; i<dislike.length;i++){
				flexTableDisLike.setText(i, 0, dislike[i]);
			}			
		}
	}

}
